package demo;

import cn.hutool.core.io.FileUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.tree.DefaultElement;
import org.dom4j.tree.DefaultText;

import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;

/**
 * @author lingkang
 * @create by 2024/4/30 16:44
 */
public class Demo22 {
    public static void main(String[] args) throws Exception {
        String file = "C:\\Users\\Administrator\\Desktop\\project\\git\\final-gateway\\conf\\config.xml";
        Document document = DocumentHelper.parseText(FileUtil.readString(file, StandardCharsets.UTF_8));
        Element root = document.getRootElement();
        List<Node> nodes = root.selectNodes("//gateway/router11");
        for (Node router : nodes) {
            DefaultElement element = (DefaultElement) router;
            System.out.println(element.nodeCount());

            Iterator<Element> elementIterator = element.elementIterator();
            while (elementIterator.hasNext()){
                DefaultElement e = (DefaultElement) elementIterator.next();
                if ("property".equals(e.getName()))
                    System.out.println(e.asXML());
                else
                    System.out.println(e);
            }
        }
    }
}
